package com.cennavi.bigdata.baotou.scripts;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.cennavi.bigdata.baotou.util.MapUtils;
import com.google.common.collect.Lists;
import org.apache.commons.io.FileUtils;
import org.junit.Test;

import java.awt.geom.Point2D;
import java.io.*;
import java.net.URL;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @author zhou
 * 按经纬度方式重新匹配大队信息
 *
 */
public class MapUtilTest {
//	交警60个高点caremaid
	public static final String jjgd60="2761,2762,2763,2764,2765,2766,2767,2768,2769,2770,2771,2772,2773,2774,2775,2776,2777,2778,2779,2780,2781,2782,2783,2784,2785,2786,2787,2788,2789,2790,2791,2792,2793,2794,2795,2796,2797,2798,2799,2800,2801,2802,2803,2804,2805,2806,2807,2808,2809,2810,2811,2812,2813,2814,2815,2816,2961,2962,2965,2966";
	
	public static List<List<Point2D.Double>> getDepartPoints() throws IOException{
		File f=new File("D:\\ddxq.MIF");
		List<List<Point2D.Double>> lists=Lists.newArrayList();
		List<Point2D.Double> points=Lists.newArrayList();
		for(String tr: FileUtils.readLines(f,"utf-8")){
			if(tr.contains("Region")){
				points=Lists.newArrayList();
				lists.add(points);
				continue;
			}
			points.add(new Point2D.Double(Double.valueOf(tr.replace("\"", "").split(" ")[0]), Double.valueOf(tr.replace("\"", "").split(" ")[1])));
		}
		return lists;
	}
	private static List<String> getDeparts() throws IOException{
		File f=new File("D:\\ddxq.MID");
		return FileUtils.readLines(f,"gb2312");
	}
//	static  String sql=
//			"select * from g_itdevices where type='shexiang' and area_id is null and ylzd6 is null ";
//;
//更新管理部门
//	static  String updateSql="update  g_itdevices set area_id=? where dev_id=? ";
//	更新坐标
	static  String updateSql2="update  g_itdevices_add set x=?,y=? where dev_id=? ";
	 static String selectSql="select count(1) as cou from g_itdevices_add where type='shexiang' and dev_id=?";
	 static String insertSql="insert into g_itdevices_add(dev_id,dev_name,ylzd4,ylzd5,create_user,create_time,update_user,update_time,del_flag,ylzd13,type)"+
			 				"values (?,?,?,?,'admin',sysdate,'admin',sysdate,'0','1','shexiang')";
	public static Connection conn = null;
	static {
		try {
			Class.forName("oracle.jdbc.driver.OracleDriver");
			 conn =DriverManager.getConnection("jdbc:oracle:thin:@(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=15.32.12.203)(PORT=1521))(ADDRESS = (PROTOCOL = TCP)(HOST = 15.32.12.203 )(PORT = 1521))(LOAD_BALANCE=yes)(CONNECT_DATA=(SERVER = DEDICATED)(SERVICE_NAME=jczhdb)))",
			 "btjczh", "btjczh1314");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
//	public static void main(String[] args) throws Exception {
//		String sessionId=HikWebserviceApi.login();
//		CmsServiceStub.CameraDTO [] cameras=HikWebserviceApi.getAllCamera2(sessionId, 1, 100000);
//		for(CmsServiceStub.CameraDTO camera:cameras){
//			if(jjgd60.indexOf(String.valueOf(camera.getCameraId()))>-1){
//				insertGItdevices(camera, 0);
//			}else{
//				insertGItdevices(camera, 1);
//			}
//		}
//	}
//	/**
//	 * @param camera
//	 * @param type 0非高点，1高点
//	 * @throws Exception
//	 */
//	private static void insertGItdevices(CmsServiceStub.CameraDTO camera,int type) throws Exception{
////		非高点
//		PreparedStatement pst=conn.prepareStatement(selectSql);
//		pst.setString(1, camera.getIndexCode());
//		ResultSet rs=pst.executeQuery();
//		rs.next();
//		String cou=rs.getString("cou");
//		if(cou.equals("0")){
//			PreparedStatement pst2=conn.prepareStatement(insertSql);
//			pst2.setString(1, camera.getIndexCode());
//			pst2.setString(2, camera.getName());
//			if(type==0){
//				pst2.setString(3, "1");
//				pst2.setString(4, "2");
//			}else{
//				pst2.setString(3, "1");
//				pst2.setString(4, "1");
//			}
//			pst2.execute();
//			conn.commit();
//			pst2.close();
//		}
//		pst.close();
//	}
//	public static void main(String[] args) throws IOException, NumberFormatException, SQLException {
//		String x="";
//		String y="";
//		String sql="select * from g_itdevices_add";
//		String updateSql="update g_itdevices_add set area_id=?,x=?,y=? where dev_id=?";
//		System.out.println(1);
//		PreparedStatement pst=conn.prepareStatement(sql);
//		ResultSet rs=pst.executeQuery();
//		String deptId="";
//		while(rs.next()){
//			x=rs.getString("x");
//			y=rs.getString("y");
//			double[] xys=TransformUtil.transform(Double.valueOf(x), Double.valueOf(y));
//			List<List<Point2D.Double>> departPoints=getDepartPoints();
//			List<String> depart=getDeparts();
//			Point2D.Double point=new Point2D.Double(xys[1], xys[0]);
//			System.out.println(xys[0]+"\t"+xys[1]);
//			for(int i=0;i<departPoints.size();i++){
//				List<Point2D.Double> points=departPoints.get(i);
//				if(MapUtils.checkPolygonInner(point, points)){
//					System.out.println(rs.getString("dev_name")+"+"+depart.get(i).replace("\"", ""));
//					deptId=depart.get(i).replace("\"", "");
//				}
//			}
//			String devId=rs.getString("dev_id");
//			PreparedStatement pst2=conn.prepareStatement(updateSql);
//			pst2.setString(1, deptId);
////			pst2.setString(2, String.valueOf(xys[1]));
////			pst2.setString(3, String.valueOf(xys[0]));
//			pst2.setDouble(2, xys[1]);
//			pst2.setDouble(3, xys[0]);
//			pst2.setString(4, devId);
//			try{
//				System.out.println(pst2.execute());
//			}catch(Exception e){
//				e.printStackTrace();;
//			}
//			conn.commit();
//			pst2.close();
//		}
//	}
//	
//	public static void main(String[] args) throws IOException, NumberFormatException, SQLException {
//		JSONObject parm=new JSONObject();
//		String mySql0="select * from s_office t";
//		PreparedStatement pst0=conn.prepareStatement(mySql0);
//		ResultSet rs0=pst0.executeQuery();
//		while(rs0.next()){
//			String name=rs0.getString("name");
//			if(!name.endsWith("大队")){
//				continue;
//			}else{
//				String key=rs0.getString("id");
//				parm.put(key, name);
//			}
//		}
//		System.out.println(parm.toString());
//	public static void main(String[] args) throws IOException, NumberFormatException, SQLException {
//		JSONObject parm=new JSONObject();
//		String mySql0="select * from s_office t";
//		PreparedStatement pst0=conn.prepareStatement(mySql0);
//		ResultSet rs0=pst0.executeQuery();
//		while(rs0.next()){
//			String name=rs0.getString("name");
//			if(name.contains("中队")){
//				continue;
//			}else{
//				String key=rs0.getString("id");
//				parm.put(key, name);
//			}
//		}
//	public static String getMapquyu(String x,String y) throws IOException, NumberFormatException, SQLException{
//
//		List<List<Point2D.Double>> departPoints=getDepartPoints();
//		List<String> depart=getDeparts();
//		Point2D.Double point=new Point2D.Double(Double.valueOf(x), Double.valueOf(y));
//		for(int i=0;i<departPoints.size();i++){
//			List<Point2D.Double> points=departPoints.get(i);
//			if(MapUtils.checkPolygonInner(point, points)){
//				System.out.println(depart.get(i).replace("\"", ""));
//				return depart.get(i).replace("\"", "");
//			}
//		}
//		return null;
//	}
	
	
	//更新大队与路段之间的关系
	public static void main(String[] args) throws IOException, NumberFormatException, SQLException {
		JSONObject parm=new JSONObject();
		String mySql0="select * from s_office t";
		PreparedStatement pst0=conn.prepareStatement(mySql0);
		ResultSet rs0=pst0.executeQuery();
		while(rs0.next()){
			String name=rs0.getString("name");
			if(name.contains("中队")){
				continue;
			}else{
				String key=rs0.getString("id");
				parm.put(key, name);
			}
		}
		
		List<JSONObject> josnList=new ArrayList<JSONObject>();
		List<List<Point2D.Double>> departPoints=getDepartPoints();
		List<String> depart=getDeparts();
		String mySql="select id,rtic,latlon,kind from comm_seg_rtic";
//		
//		PreparedStatement pst=conn.prepareStatement(sql);
		PreparedStatement pst=conn.prepareStatement(mySql);
		ResultSet rs=pst.executeQuery();
		int count=0;
		
		while(rs.next()){
			
			String[] lonlat=rs.getString("latlon").split(",");
//			String x = rs.getString("x");
//			String y = rs.getString("y");
			String x = lonlat[0];
			String y = lonlat[1];
//			//String x="109.8434166666670110";
//			//String y="40.6556833333333003";
			Point2D.Double point=new Point2D.Double(Double.valueOf(x), Double.valueOf(y));
			for(int i=0;i<departPoints.size();i++){
				List<Point2D.Double> points=departPoints.get(i);
				if(MapUtils.checkPolygonInner(point, points)){
					count++;
					//System.out.println("所属大队："+depart.get(i).replace("\"", ""));
					JSONObject json=new JSONObject();
					String id = rs.getString("id");
					json.put("roadID", id);
					json.put("rticID", rs.getString("rtic"));
					json.put("lonlat", rs.getString("latlon"));
					json.put("kind", rs.getString("kind"));
					json.put("jcID", depart.get(i).replace("\"", ""));
					json.put("jcName", parm.get(depart.get(i).replace("\"", "")));
					josnList.add(json);
					
					
//					PreparedStatement pst2=conn.prepareStatement(updateSql);
//					pst2.setString(1, depart.get(i).replace("\"", ""));
//					pst2.setString(2, rs.getString("DEV_ID"));
//					pst2.execute();
//					pst2.close();
					break;
					}
				}
			}
			File fileout = new File("D:\\rtic.txt");
			BufferedWriter writer = new BufferedWriter(new FileWriter(fileout));
			writer.write(josnList.toString());
			writer.close();
			System.out.println(count);
//			JSONArray array=JSONArray.fromObject(josnList);
			System.out.println(josnList.toString());
	}


	//中奥程序跑出的数据保存在resource下rtic.txt文件中,处理后的数据保存在rtic_jc_map.txt文件中
	//有时候会跑出乱码，注意(经过多次对比分析，System.out.println(sb.toString());这句执行就乱码，不执行就不乱吗。
	@Test
	public void testasd() throws Exception {
		URL url = ClassLoader.getSystemResource("rtic.txt");
		File file = new File(url.getFile());
		System.out.println(file.getPath());
		StringBuilder sb = new StringBuilder();
		BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream(),"UTF-8"));
		String tempString = null;
		while ((tempString = reader.readLine()) != null) {
			sb.append(tempString);
		}
		reader.close();
		//System.out.println(sb.toString());
		JSONArray array = JSON.parseArray(sb.toString());
		int len = array.size();
		JSONObject json = new JSONObject();
		List<Object> names=new ArrayList<Object>();
		for (int i = 0; i < len; ++i) {
			JSONObject temp = array.getJSONObject(i);
			JSONObject temp0 = new JSONObject();
			temp0.put("jcID", temp.getString("jcID"));
			temp0.put("jcName", temp.getString("jcName"));
			if(!names.contains(temp.getString("jcName"))){
				names.add(temp.getString("jcName"));
			}
			temp0.put("kind", temp.getString("kind"));
			json.put(temp.getString("lonlat") + "," + temp.getString("roadID"), temp0);
		}
		//System.out.println(new JSONArray(names).toJSONString());
		System.out.println(json.toJSONString());
//		System.out.println(json.keySet().size());
	}

}
